//
//  CustomView.m
//  封装UIView
//
//  Created by adiljan on 16/9/2.
//  Copyright © 2016年 adiljan. All rights reserved.
//


/**
 *  CustomView.m文件
 */
#import "CustomView.h"
#import "CustomModel.h"
#import "CustomTool.h"

//颜色将16位值转换成rgb值,UIColor与字符串互转
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue &0xFF00) >>8))/255.0 blue:((float)(rgbValue &0xFF))/255.0 alpha:1.0]

@interface CustomView ()
// 步骤2 在刚刚新建类的`类扩展`中添加子控件属性（用`weak`声明，防止内存泄露）
@property (nonatomic, weak) UIImageView *iconImageView;

@property (nonatomic, weak) UILabel *nameLabel;

@property (nonatomic, weak) UIButton *btnClick;

@end

@implementation CustomView

// 步骤3 在initWithFrame:方法中添加子控件
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        // 注意：该处不要给子控件设置frame与数据，可以在这里初始化子控件的属性
        UIImageView *iconImageView = [[UIImageView alloc] init];
        self.iconImageView = iconImageView;
        [self addSubview:iconImageView];
        
        UILabel *nameLabel = [[UILabel alloc] init];
        // 设置子控件的属性
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = [UIFont systemFontOfSize:10];
        self.nameLabel = nameLabel;
        [self addSubview:nameLabel];
        
        UIButton *btnClick = [[UIButton alloc]init];
        self.btnClick = btnClick;
        btnClick.backgroundColor = [UIColor clearColor];
        
        [btnClick addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:btnClick];
    }
    return self;
}

- (void)btnClick:(UIButton *)sender{
//    if (_BtnClickBlock) {
//        _BtnClickBlock();
//    }
    
    [self.myDelegate clickButton:sender];
}
// 步骤4 在`layoutSubviews`方法中设置子控件的`frame`（在该方法中一定要调用`[super layoutSubviews]`方法）
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat iconImageViewX = 0;
    CGFloat iconImageViewY = 0;
    CGFloat iconImageViewW = self.bounds.size.width;
    CGFloat iconImageViewH = 80;
    self.iconImageView.frame = CGRectMake(iconImageViewX, iconImageViewY, iconImageViewW, iconImageViewH);
    
    CGFloat nameLabelX = 0;
    CGFloat nameLabelY = iconImageViewH;
    CGFloat nameLabelW = iconImageViewW;
    CGFloat nameLabelH = self.bounds.size.height - iconImageViewH;
    self.nameLabel.frame = CGRectMake(nameLabelX, nameLabelY, nameLabelW, nameLabelH);
    
    CGFloat btnClickX = 0;
    CGFloat btnClickY = 0;
    CGFloat btnClickW = iconImageViewW;
    CGFloat btnClickH = self.bounds.size.height;
    self.btnClick.frame = CGRectMake(btnClickX, btnClickY, btnClickW, btnClickH);
}

// 步骤6 在该`setter`方法中取出模型属性，给对应的子控件赋值
- (void)setModel:(CustomModel *)model
{
    _model = model;
    
    self.iconImageView.image = [UIImage imageNamed:model.icon];
    self.nameLabel.text = model.name;
    
    self.nameLabel.textColor = [CustomTool colorWithHexString:model.titleColor];
//    self.nameLabel.textColor =UIColorFromRGB(0x1886FE);

    
}


@end
